package com.cn.base;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



/**
 * @ClassName: BaseServiceImpl
 * @Description: TODO
 * @author Administrator
 * @date 2017年6月8日
 */
@Transactional(readOnly = true)
public abstract class BaseServiceImpl <D extends BaseDao<T>, T extends BaseEntity<T>> implements BaseService<T> {
	

	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;
	
	@Override
	public T get(Map<String, Object> params){
		return dao.get(params);
	}
	
	@Override
	@Transactional(readOnly = false)
	public int deleteByParams(Map<String, Object> params){
		return dao.delete(params);
	}
	
	@Override
	@Transactional(readOnly = false)
	public int save(T entity) throws Exception{
		return dao.save(entity);
	}
	
	@Override
	@Transactional(readOnly = false)
	public int update(T entity){
		return dao.update(entity);
	}
	
	/**
	 * 分页 Dao需实现queryList 以及对应的xml
	 * @Title: findPage
	 * @Description: TODO
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @param params
	 * @param @return
	 * @return PageInfo<T>
	 * @throws
	 */
	@Override
	public PageInfo<T> findPage(Map<String, Object> params){
		int pageNum = Integer.parseInt(params.get("page").toString());
        int pageSize = Integer.parseInt(params.get("limit").toString());
		PageHelper.startPage(pageNum, pageSize);
		List<T> data=dao.queryList(params);
		PageInfo<T> list = new PageInfo<T>(data);
		return list;
	}
	
	@Override
	public List<T> findList(Map<String, Object> params){
		List<T> list=dao.queryList(params);
		return list;
	}
	
	/* (非 Javadoc)
	 * 
	 * 
	 * @param entity
	 * @return
	 * @see com.cn.utils.crud.BaseService#get(com.cn.utils.crud.BaseEntity)
	 */
	    
	@Override
	public T get(T entity) {
		
		return null;
	}


	@Transactional(readOnly = false)
	@Override
	public int deleteByIds(String[] ids) throws Exception {
		return dao.deleteBatch(ids);
	}

}
